﻿using System;
using Hammock;
using Harvest.Api.Interfaces;

namespace Harvest.Api
{

    public abstract class RepositoryBase
    {

        protected IRestRequestProvider Provider { get; private set; }


        protected RepositoryBase(IRestRequestProvider provider)
        {
            Provider = provider;
        }


        internal RestClient GetRestClient()
        {
            var client = new RestClient
                             {
                                 Authority = "https://" + Provider.Account + ".harvestapp.com"
                             };
            client.AddHeader("Accept", "application/xml");
            client.AddHeader("Content-Type", "application/xml");
            client.AddHeader("Authorization", "Basic " + GetAuthorizationString(Provider.Login, Provider.Password));
            return client;
        }


        private static string GetAuthorizationString(string login, string password)
        {
            return
                Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(login + ":" + password)).Replace("\r", "").Replace("\n", "");
        }

    }

}
